package com.aivin.customView;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;


import com.aivin.maproutelib.tool.WkToastTools;
import com.amap.api.maps.AMap;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.aivin.maproutelib.manager.MapRouteManager;
import com.aivin.maproutelib.bean.GaoDeMarker;
import com.aivin.maproutelib.bean.WkGDmap;
import com.aivin.maproutelib.tool.WkLogTool;
import com.aivin.planningdemo.R;

import java.util.List;

/**
 *  航线规划参数设置界面
 */
public class MyLineSettingView extends LinearLayout   {

    private Context mContext = null;
    private EditText edHeightTx;
    private EditText edWidthTx;

    public MyLineSettingView(Context context) {
        super(context);
        initView(context);
    }

    public MyLineSettingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyLineSettingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.bar_right_way_line_view, this);
        view.findViewById(R.id.btn_clear_all).setOnClickListener(onClickListener);
        view.findViewById(R.id.btn_goback).setOnClickListener(onClickListener);
        view.findViewById(R.id.image_btn_add_degree).setOnClickListener(onClickListener);
        view.findViewById(R.id.image_btn_sub_degree).setOnClickListener(onClickListener);
        view.findViewById(R.id.btn_auto_line).setOnClickListener(onClickListener);
        view.findViewById(R.id.btn_create_line).setOnClickListener(onClickListener);
        view.findViewById(R.id.btn_exit).setOnClickListener(onClickListener);
        view.findViewById(R.id.btn_start_way).setOnClickListener(onClickListener);

        edHeightTx = view.findViewById(R.id.et_height);
        edHeightTx.addTextChangedListener(new OnTextClickEvent(edHeightTx));
        edWidthTx = view.findViewById(R.id.et_width);
        edWidthTx.addTextChangedListener(new OnTextClickEvent(edWidthTx));
    }

    public void initMapObject(Context context ,WkGDmap wkGDmap, TextureMapView textureMapView  ) {
        MapRouteManager.getInstance().initMap( context, wkGDmap);
        textureMapView.getMap().setOnMarkerClickListener(onMarkerClickListener);
    }


    private OnClickListener onClickListener =new OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_goback:
                    MapRouteManager.getInstance().markerGoback();
                    break;
                case R.id.btn_clear_all:
                    clearMapRouteManager();
                    break;
                case R.id.image_btn_add_degree:
                    MapRouteManager.getInstance().addDirectionAngle();
                    break;
                case R.id.image_btn_sub_degree:
                    MapRouteManager.getInstance().subDirectionAngle();
                    break;
                case R.id.btn_auto_line:
                    // 对齐边
                    MapRouteManager.getInstance().autoLine(true);
                    break;
                case R.id.btn_create_line:
                    // 生成线
                    MapRouteManager.getInstance().createLine();
                    break;
                case R.id.btn_start_way:
                    List<LatLng> flayPoints = MapRouteManager.getInstance().getAllPointList();
                    if (flayPoints.size() <= 0) {
                        WkToastTools.toastInfo(mContext, mContext.getResources().getString(R.string.lo_please_create_line));
                        return;
                    } else {
                        WkToastTools.toastInfo(mContext, String.format("获得%s个航点数据，可以发送指令",String.valueOf(flayPoints.size())));
                    }
                    break;
                case R.id.btn_exit:
                    clearMapRouteManager();
                    WkToastTools.toastInfo(mContext,  "exit!");
                    break;
                default:
                    break;
            }
        }
    };


    private AMap.OnMarkerClickListener onMarkerClickListener = new AMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            MapRouteManager.getInstance().updateSelectMarkerIndexAndIcon(new GaoDeMarker(marker) ) ;
            return true;
        }
    };

    /**
     * 清除航线 航点ui和数据
     */
    public void clearMapRouteManager(){
        MapRouteManager.getInstance().clearAll();
    }



    private class OnTextClickEvent implements TextWatcher {
        private EditText mEditText;

        private OnTextClickEvent(EditText editText) {
            this.mEditText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)  {
            switch (mEditText.getId()) {
                case R.id.et_height:
                    String edHeightStr = edHeightTx.getText().toString();
                    WkLogTool.showLog(String.format("航线高度=%s" ,edHeightStr));

                    break;
                case R.id.et_width:
                    String edWidthStr = edWidthTx.getText().toString();
                    if (!TextUtils.isEmpty(edWidthStr)) {
                        MapRouteManager.getInstance().setSprayBlessings(Double.parseDouble(edWidthStr));
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {    }
    }

}
